//index.js
//获取应用实例
const app = getApp()

var apiHost = wx.getStorageSync("apiHost"); //请求地址

var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({
  data: {
    indicatorDots: false,   //是否显示面板指示点（轮播图）
    autoplay: true,        //是否自动切换（轮播图）
    interval: 6000,         //自动切换时间间隔（轮播图）
    duration: 1000,         //滑动动画时长（轮播图）
    showCancelArea: false,   //是否展示领劵页面
    bannners:'',            //轮模图
    eightModel : '',        //八大模块
    specialTime : '',       //特卖时间
    specialGood : '',       //特卖商品
    specialId : '',
  },
 
  onLoad: function () {

      var that = this;
      that.getBanners();
      that.getEightModel();
      that.getSpecialTime();

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  // 获取首页轮播
  getBanners : function(){
    var that = this;
    wx.request({
      url: apiHost +"/mobile/goods/banners", //仅为示例，并非真实的接口地址
      data: {
        'type':'yh'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          that.setData({
            bannners: res.data.data
          })
        }else{
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 获取八大模块
   */
  getEightModel : function(){
    var that = this;
    wx.request({
      url: apiHost + "/mobile/goods/xcxmodels", //仅为示例，并非真实的接口地址
      data: {
        'token': 'yh',
         appid:''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        console.log(res)
        if (code == "10000") {
          that.setData({
            eightModel: res.data.data
          })
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 获取特卖时间
   */
  getSpecialTime : function(){
    var that = this;
    wx.request({
      url: apiHost + "/mobile/market/specialTime", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          var getDateLength = res.data.data.length;
          for (var i = 0; i < getDateLength; i++) {
            res.data.data[i].time = res.data.data[i].time.substring(0, 5);
            if (res.data.data[i].state==1){
              that.setData({
                specialId: res.data.data[i].id
              })
            }
          }
          var specialid = that.data.specialId;
          if (specialid != '') {
            that.getSpecialGoods(specialid);  //获取 特卖商品
          }
          that.setData({
            specialTime: res.data.data
          })
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 点击特卖时间
   */
  selectSpecial:function(e){
    var that = this;
    var specialid = e.currentTarget.dataset.specialid
    that.setData({
      specialId: specialid
    })
    console.log(specialid)
    if (specialid!=''){
      that.getSpecialGoods(specialid);  //获取 特卖商品
    }
  },

  /**
   * 获取 特卖商品
   */
  getSpecialGoods : function(id){
    var that = this;
    wx.request({
      url: apiHost + "/mobile/market/specialGood", //仅为示例，并非真实的接口地址
      data: {
        id: id,
        pageNo:1,
        pageSize:10
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          console.log(res.data.data)
          that.setData({
            specialGood: res.data.data
          })
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 跳转商品详情
   */
  goYhGoodsDetail: function (e) {
    var that = this;
    var spuid = e.currentTarget.dataset.spuid;

    wx.navigateTo({
      url: "../goods/yhjGoodsDetail/yhjGoodsDetail?spuid=" + spuid,
    })
  },


  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  gojd: function () {
    wx.navigateTo({
      url: "../activityTopic/jd/jd",
    })
  },
  goyh: function () {
    wx.navigateTo({
      url: "../activityTopic/yhj/yhj",
    })
  },
  goNewPeople: function () {
    wx.navigateTo({
      url: "../activityTopic/newPeople/newPeople",
    })
  },
  goHCollage: function () {
    wx.navigateTo({
      url: "../activityTopic/hCollage/hCollage",
    })
  },
  /**
   * 关闭领劵框按钮
   */
  colseModel: function () {
    var that = this;
    that.setData({
      showCancelArea: false
    });
  },
  getUserInfo: function(e) {
    
  },
  /***
   * 搜索商品  跳搜索订单页
  */
  searchInput: function (e) {
    var value = e.detail.value;
    this.goPage('../goods/searchgoods/searchgoods?value='+value)
  },
  /**
 * 跳转页面
 */
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();
      }
    })
  },
  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})
